import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import * as authActions from 'redux/modules/auth';
import store from 'store';
import { push } from 'react-router-redux';

@connect(
  state => ({
    user: state.auth.user,
    isAuthenticated: state.auth.isAuthenticated
  }), {...authActions, pushState: push })
export default class Authenticate extends Component {
  static propTypes = {
    children: PropTypes.object.isRequired,
    user: PropTypes.object,
    isAuthenticated: PropTypes.bool,
    pushState: PropTypes.func.isRequired,
    getCurrentUserBySessionToken: PropTypes.func,
    initUser: PropTypes.func
  }

  componentWillMount() {
  }

  componentWillReceiveProps() {
    console.log('子组件变更...');
    this.checkAuth(this.props.isAuthenticated, this.props.user);
  }

  checkAuth(isAuthenticated, user) {
    if (!user) {
      const sessionToken = store.get('sessionToken');
      console.log('没有user');
      if (sessionToken) {
        this.props.getCurrentUserBySessionToken(sessionToken);
      }
    }
  }

  render() {
    return (
      <div id="authenticate">
         {this.props.children}
      </div>
    );
  }
}
